package com.binfenhulian.chinaenergy.ces.utils.binary;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于查找更换二进制位值 Created by IntelliJ IDEA. User: BaiSheng Date: 2010-4-26 Time: 15:57:10
 */
public class BinaryUtil {
	private static final int defaultNumLen = 50;
	
	/**
	 * 判断二进制中的指定位是1||0
	 * 
	 * @param value 要判断的数字
	 * @param bitPos 指定的位置
	 * @return boolean 值
	 */
	public static boolean bitIs1(int value, int bitPos) {
		int temp = value >> (bitPos - 1);
		
		temp &= 0x0001;
		
		return temp != 0;
		
	}
	
	public static boolean bitIs1(Long value, int bitPos) {
		Long temp = value >> (bitPos - 1);
		temp &= 0x0001;
		return temp != 0;
		
	}
	
	/**
	 * 判断二进制中的指定位是否为0
	 * 
	 * @param value 要判断的数字
	 * @param bitPos 指定的位置
	 * @return boolean 值
	 */
	public static int bitIs0(int value, int bitPos) {
		int temp = value >> (bitPos - 1);
		temp &= 0x0001;
		return temp;
	}
	
	public static long bitIs0(long value, int bitPos) {
		long temp = value >> (bitPos - 1);
		temp &= 0x0001;
		return temp;
	}
	
	/**
	 * 更改指定位值
	 * 
	 * @param bitPos 指定位
	 * @return 更改后的值
	 */
	public static int changeBitValue(int value, int bitPos) {
		// bit pos 从低位算起，最低为1， 例数第二为2.
		int temp = (int) (Math.pow(2, bitPos - 1));
		
		return value ^ temp;
	}
	
	public static Long changeBitValue(Long value, int bitPos) {
		// bit pos 从低位算起，最低为1， 例数第二为2.
		long temp = (long) (Math.pow(2, bitPos - 1));
		
		return value ^ temp;
	}
	
	public static int arrayToBitInt(String[] st) {
		if (st == null || st.length == 0)
			return 0;
		String re = "";
		for (int i = 0; i < st.length; i++) {
			if ("on".equals(st[i])) {
				re = "1" + re;
			} else {
				re = "0" + re;
			}
		}
		return Integer.valueOf(re, 2);
	}
	
	public static long arrayChangeBiner(String[] st) {
		if (st == null || st.length == 0)
			return 0;
		String[] cc = new String[defaultNumLen];
		for (int i = 0; i < cc.length; i++) {
			cc[i] = "0";
		}
		for (int i = 0; i < st.length; i++) {
			int s = Integer.parseInt(st[i]);
			cc[cc.length - s] = "1";
		}
		String re = "";
		for (int i = 0; i < cc.length; i++) {
			re += cc[i];
		}
		return Long.valueOf(re, 2);
	}
	
	public static String[] binerchangearray(int nu) {
		return binerchangearray(nu, defaultNumLen);
	}
	
	public static String[] binerchangearray(long nu) {
		return binerchangearray(nu, defaultNumLen);
	}
	
	/**
	 * 解析nu成二进制数组
	 * 
	 * @param nu 值
	 * @param len 多少位
	 * @return
	 */
	public static String[] binerchangearray(int nu, int len) {
		List<String> re = new ArrayList<String>();
		for (int i = 0; i < len; i++) {
			if (bitIs1(nu, i)) {
				re.add("" + i);
			}
		}
		String[] a = new String[re.size()];
		return re.toArray(a);
	}
	
	public static String[] binerchangearray(long nu, int len) {
		List<String> re = new ArrayList<String>();
		for (int i = 0; i < len; i++) {
			if (bitIs1(nu, i)) {
				re.add("" + i);
			}
		}
		String[] a = new String[re.size()];
		return re.toArray(a);
	}
	
	public static String[] binerArray(int nu, int len) {
		List<String> re = new ArrayList<String>();
		for (int i = 0; i < len; i++) {
			BinaryUtil butil = new BinaryUtil();
			if (butil.bitIs1(nu, i + 1)) {
				re.add("true");
			} else {
				re.add("false");
			}
		}
		return re.toArray(new String[re.size()]);
	}
	
	/**
	 * 解析nu成二进制数组
	 * 
	 * @return
	 */
	public static String arrayToJsonShow(String[] values, String[] name) {
		StringBuffer re = new StringBuffer("[{");
		if (values != null && values.length > 0) {
			re.append("name:'").append(name[0]).append("',value:").append(values[0])
					.append("}");
			for (int i = 1; i < name.length; i++) {
				re.append(",{name:'").append(name[i]).append("',value:")
						.append(values[i]).append("}");
			}
			re.append("]");
		} else {
			return "[]";
		}
		return re.toString();
	}
	
	/**
	 * @Description: 将部分区域的二进制值修改为某个数，比如将m的2-8位的值修改n
	 * @param value 要修改的数值
	 * @param start 区间开始位，最低位为1
	 * @param end 区间结束位
	 * @param cValue 需要将区间值调整成的数值
	 * @return
	 * @return Integer
	 * @author ylx
	 * @date 2012-7-30 下午03:17:22
	 */
	public static Integer changeBitAreas(int value, int start, int end, int cValue) {
		Integer result = null;
		
		try {
			for (int i = start; i <= end; i++) {
				if (bitIs1(cValue, i - start + 1)) {
					if (!bitIs1(value, i)) {
						value = changeBitValue(value, i);
					}
				} else {
					if (bitIs1(value, i)) {
						value = changeBitValue(value, i);
					}
				}
			}
			result = value;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * @Description: 获取value某个范围的值start-end
	 * @param value
	 * @param start 区间开始位，最低位为1
	 * @param end 区间结束位
	 * @return
	 * @return Integer
	 * @author ylx
	 * @date 2012-8-17 下午03:21:33
	 */
	public static Integer getAreasValue(int value, int start, int end) {
		int result = 0;
		try {
			for (int i = start; i <= end; i++) {
				if (bitIs1(value, i)) {
					result = changeBitValue(result, i - start + 1);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	

}
